package com.dmx.cep.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.dmx.cep.dao.ConfBlockDao;
import com.dmx.cep.entity.ConfBlock;
import com.dmx.cep.entity.ConfBlockUpdate;
import com.dmx.fw.dao.hibernateimpl.BaseDaoImpl;

public class ConfBlockDaoImpl extends BaseDaoImpl implements ConfBlockDao {

    public void batchDeleteConfBlockByIds(Integer[] ids){
    	if(ids==null){
    		throw new IllegalArgumentException("arg ids couldn't be null for ConfBlockDaoImpl.batchDeleteConfBlockByIds");
    	}
        if (ids != null) {
            for(Integer id:ids){
               deleteConfBlockById(id);
            }
        }
    }

    public void deleteConfBlockById(Integer id){
    	ConfBlock confBlock =findConfBlockById(id);
    	if(confBlock!=null){
    		if(confBlock.getBlocktype()==1&&confBlock.getBlackorwhite()==1){
    			//关键字、白名单过滤
    			ConfBlockUpdate confUpdate=(ConfBlockUpdate)getHibernateTemplate().get(ConfBlockUpdate.class, "key-white");
    			if(confUpdate!=null){
        			confUpdate.setUpdateTime(new Date());
        			getHibernateTemplate().saveOrUpdate(confUpdate);
    			}

    		}
    		if(confBlock.getBlocktype()==1&&confBlock.getBlackorwhite()==0){
    			//关键字、黑名单过滤
    			ConfBlockUpdate confUpdate=(ConfBlockUpdate)getHibernateTemplate().get(ConfBlockUpdate.class, "key-black");
    			if(confUpdate!=null){
        			confUpdate.setUpdateTime(new Date());
        			getHibernateTemplate().saveOrUpdate(confUpdate);
    			}
    		}
    		if(confBlock.getBlocktype()==0&&confBlock.getBlackorwhite()==1){
    			//域名、白名单过滤
    			ConfBlockUpdate confUpdate=(ConfBlockUpdate)getHibernateTemplate().get(ConfBlockUpdate.class, "domain-white");
    			if(confUpdate!=null){
        			confUpdate.setUpdateTime(new Date());
        			getHibernateTemplate().saveOrUpdate(confUpdate);
    			}
    		}
    		if(confBlock.getBlocktype()==0&&confBlock.getBlackorwhite()==0){
    			//域名、黑名单过滤
    			ConfBlockUpdate confUpdate=(ConfBlockUpdate)getHibernateTemplate().get(ConfBlockUpdate.class, "domain-black");
    			if(confUpdate!=null){
        			confUpdate.setUpdateTime(new Date());
        			getHibernateTemplate().saveOrUpdate(confUpdate);
    			}
    		}
    		
    		super.getHibernateTemplate().delete(confBlock);
    	}
    }

    public void saveOrUpdateConfBlock(ConfBlock ConfBlock){
        super.getHibernateTemplate().saveOrUpdate(ConfBlock);
    }

    @SuppressWarnings("unchecked")
	public ConfBlock findConfBlockById(Integer id){
    	if(id==null){
    		throw new IllegalArgumentException("arg id couldn't be null for ConfBlockDaoImpl.deleteConfBlockById");
    	}
        String hql = "from ConfBlock where blockid =?";
        List<ConfBlock> ConfBlocks = this.getHibernateTemplate().find(hql,id);
        if(!ConfBlocks.isEmpty())
          return ConfBlocks.get(0);
        return null;
    }

	@Override
	public void saveOrUpdateBlackWhite(Integer keyOrDomain, Integer blackOrWhite,
			Integer isValid, String value) throws DataAccessException {
		// TODO Auto-generated method stub
		List<ConfBlock> list=this.getHibernateTemplate().find(
				"from ConfBlock c where c.blocktype=? and c.blackorwhite=? and c.value=?",new Object[]{new Integer(keyOrDomain),new Integer(blackOrWhite),value});
		if(list.size()>0){
			ConfBlock confBlock=list.get(0);
			confBlock.setFlag(isValid);
			this.getHibernateTemplate().saveOrUpdate(confBlock);
		}else{
			ConfBlock confBlock=new ConfBlock();
			confBlock.setBlocktype(keyOrDomain);
			confBlock.setBlackorwhite(blackOrWhite);
			confBlock.setFlag(isValid);
			confBlock.setValue(value);
			this.getHibernateTemplate().saveOrUpdate(confBlock);
		}
		
	}

	@Override
	public List<Object[]> queryBlackWhiteList(final String keyOrDomain,
			final String blackOrWhite, final String isValid, final String value,
			final String orderByScript) throws Exception {
		// TODO Auto-generated method stub
		return (List<Object[]>)this.getHibernateTemplate().execute(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				List<Object> pars=new ArrayList<Object>();
				String sql="select tcb.BLOCKTYPE,tcb.BLACKORWHITE,tcb.FLAG, tcb.VALUE from t_conf_block tcb where 1 = 1";
				if(keyOrDomain!=null&&!keyOrDomain.trim().equals("")){
					sql+=" and tcb.BLOCKTYPE=?";
					pars.add(keyOrDomain);
				}
				if(blackOrWhite!=null&&!blackOrWhite.trim().equals("")){
					sql+=" and tcb.BLACKORWHITE=?";
					pars.add(blackOrWhite);
				}
				if(isValid!=null&&!isValid.trim().equals("")){
					sql+=" and tcb.FLAG=?";
					pars.add(isValid);
				}
				if(value!=null&&!value.trim().equals("")){
					sql+=" and tcb.VALUE like ?";
					pars.add("%"+value+"%");
				}
				
				if(orderByScript!=null&&!orderByScript.trim().equals("")){
					sql+=" "+orderByScript;
				}else{
					sql+=" order by tcb.BLOCKID desc";
				}
				
				SQLQuery query=session.createSQLQuery(sql);
				
				for(int i=0;i<pars.size();i++){
					query.setParameter(i, pars.get(i));
				}
				
				List<Object[]> result=query.list();
				
				return result;
			}});
	}
}
